﻿Public Class frmAD_CapNhatTinBai 

    Private Sub SimpleButton2_Click(sender As System.Object, e As System.EventArgs) Handles SimpleButton2.Click
        Me.Close()
    End Sub
    Public _dangtinDTO As New DangTinDTO
    Public _dangtinBUS As New DangTinBUS
    Dim diachiDTO As New DiaChiDTO
    Dim diachiBUS As New DiaChiBUS
    Public tableLoaiNha As New DataTable
    Public tableDiaChi As New DataTable
    Public tableLoaiTin As New DataTable
    Public tableKhuVuc As New DataTable
    Private Sub frmAD_CapNhatTinBai_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        If _dangtinDTO.TinhTrang = 1 Then
            cbTrangThai.Items.Add("Đã duyệt")
        ElseIf _dangtinDTO.TinhTrang = 2 Then
            cbTrangThai.Items.Add("Chưa duyệt")
        ElseIf _dangtinDTO.TinhTrang = 3 Then
            cbTrangThai.Items.Add("Tạm đóng")
        ElseIf _dangtinDTO.TinhTrang = 4 Then
            cbTrangThai.Items.Add("Đã xóa")
        End If
        cbTrangThai.Items.Add("Đã duyệt")
        cbTrangThai.Items.Add("Chưa duyệt")
        cbTrangThai.Items.Add("Tạm đóng")
        cbTrangThai.Items.Add("Đã xóa")
        cbTrangThai.SelectedIndex = 1
        '----------------------------------------------------------------------------------------------------
        Dim loaiNhaDTO As New LoaiNhaDTO
        Dim loaiNhaBUS As New LoaiNhaBUS
        '----------------------------------------------------------------------------------------------------
        Dim loaitinDTO As New LoaiTinDTO
        Dim loaitinBUS As New LoaiTinBUS
        '----------------------------------------------------------------------------------------------------
        Dim kvDTO As New KhuVucDTO
        Dim kvBUS As New KhuVucBUS
        '----------------------------------------------------------------------------------------------------
        tableLoaiTin = loaitinBUS.GetAll()
        tableLoaiNha = loaiNhaBUS.GetAll()
        tableKhuVuc = kvBUS.GetAll()
        tableDiaChi = diachiBUS.GetAll()
        '------------------------------------------------------------------------------------------
        For i As Integer = 0 To tableDiaChi.Rows.Count - 1
            If Convert.ToInt32(tableDiaChi.Rows(i)("MaDiaChi").ToString()) = _dangtinDTO.MaDiaChi Then
                diachiDTO.MaDiaChi = Convert.ToInt32(tableDiaChi.Rows(i)("MaDiaChi").ToString())
                diachiDTO.Duong = tableDiaChi.Rows(i)("Duong").ToString()
                diachiDTO.Phuong = tableDiaChi.Rows(i)("Phuong").ToString()
                diachiDTO.Quan = tableDiaChi.Rows(i)("Quan").ToString()
                diachiDTO.SoNha = tableDiaChi.Rows(i)("SoNha").ToString()
                Exit For
            End If
        Next
        '------------------------------------------------------------------------------------------
        For i As Integer = 0 To tableLoaiNha.Rows.Count - 1
            If Convert.ToInt32(tableLoaiNha.Rows(i)("MaLoaiNha").ToString()) = _dangtinDTO.MaLoaiNha Then
                cbLoaiNha.Items.Add(tableLoaiNha.Rows(i)("TenLoaiNha"))
                Exit For
            End If
        Next
        cbLoaiNha.SelectedIndex = 0
        '---------------------------------------------------------------------------------------------------
        For i As Integer = 0 To tableLoaiTin.Rows.Count - 1
            If Convert.ToInt32(tableLoaiTin.Rows(i)("MaLoaiTin").ToString()) = _dangtinDTO.MaLoaiTin Then
                cbLoaiTin.Items.Add(tableLoaiTin.Rows(i)("TenLoai"))
                Exit For
            End If
        Next
        cbLoaiTin.SelectedIndex = 0
        '----------------------------------------------------------------------------------------------------
        For i As Integer = 0 To tableKhuVuc.Rows.Count - 1
            If tableKhuVuc.Rows(i)("TenKhuVuc").ToString() = diachiDTO.Quan Then
                cbQuan.Items.Add(tableKhuVuc.Rows(i)("TenKhuVuc"))
                Exit For
            End If
        Next
        'cbQuan.SelectedIndex = 0
        cbQuan.Text=diachiDTO.Quan
        '----------------------------------------------------------------------------------------------------
        For i As Integer = 0 To tableKhuVuc.Rows.Count - 1
            cbQuan.Items.Add(tableKhuVuc.Rows(i)("TenKhuVuc"))
        Next

        For i As Integer = 0 To tableLoaiNha.Rows.Count - 1
            cbLoaiNha.Items.Add(tableLoaiNha.Rows(i)("TenLoaiNha"))
        Next

        For i As Integer = 0 To tableLoaiTin.Rows.Count - 1
            cbLoaiTin.Items.Add(tableLoaiTin.Rows(i)("TenLoai"))
        Next
        '----------------------------------------------------------------------------------------------------
        cbSuaChua.Items.Add(_dangtinDTO.SucChua)
        cbSuaChua.SelectedIndex = 0
        '----------------------------------------------------------------------------------------------------
        cbSuaChua.Items.Add("1")
        cbSuaChua.Items.Add("2")
        cbSuaChua.Items.Add("3")
        cbSuaChua.Items.Add("4")
        cbSuaChua.Items.Add("5")
        cbSuaChua.Items.Add("6")
        cbSuaChua.Items.Add("7")
        cbSuaChua.Items.Add("8")
        cbSuaChua.Items.Add("9")
        cbSuaChua.Items.Add("10")
        '----------------------------------------------------------------------------------------------------
        txtDienTich.Text = _dangtinDTO.DienTich
        txtGiaThue.Text = _dangtinDTO.GiaThue
        txtTieuDe.Text = _dangtinDTO.TieuDe
        txtPhuong.Text = diachiDTO.Phuong
        txtDuong.Text = diachiDTO.Duong
        txtSoNha.Text = diachiDTO.SoNha
        '----------------------------------------------------------------------------------------------------
        If _dangtinDTO.ChoNamThue = 1 Then
            ckChoNamThue.Checked = True
        Else
            ckChoNamThue.Checked = False
        End If
        '----------------------------------------------------------------------------------------------------
        If _dangtinDTO.ChoNuThue = 1 Then
            ckChoNuThue.Checked = True
        Else
            ckChoNuThue.Checked = False
        End If
        '----------------------------------------------------------------------------------------------------
        If _dangtinDTO.GanCho = 1 Then
            ckGanCho.Checked = True
        Else
            ckGanCho.Checked = False
        End If
        '----------------------------------------------------------------------------------------------------
        If _dangtinDTO.ChoNauAn = 1 Then
            ckNauAn.Checked = True
        Else
            ckNauAn.Checked = False
        End If
        '----------------------------------------------------------------------------------------------------
        If _dangtinDTO.OChungChu = 1 Then
            ckChungChu.Checked = True
        Else
            ckChungChu.Checked = False
        End If
        '----------------------------------------------------------------------------------------------------
        If _dangtinDTO.OGhep = 1 Then
            ckOGhep.Checked = True
        Else
            ckOGhep.Checked = False
        End If
        '----------------------------------------------------------------------------------------------------
        If _dangtinDTO.Internet = 1 Then
            ckInternet.Checked = True
        Else
            ckInternet.Checked = False
        End If
        '----------------------------------------------------------------------------------------------------
        If _dangtinDTO.TruyenHinhCap = 1 Then
            ckTruyenHinhCap.Checked = True
        Else
            ckTruyenHinhCap.Checked = False
        End If
        '----------------------------------------------------------------------------------------------------
        If _dangtinDTO.GanTramXeBuyt = 1 Then
            ckXeBuyt.Checked = True
        Else
            ckXeBuyt.Checked = False
        End If
    End Sub

    Private Sub btnThemMoi_Click(sender As System.Object, e As System.EventArgs) Handles btnThemMoi.Click
        _dangtinDTO.DienTich = Convert.ToSingle(txtDienTich.Text)
        _dangtinDTO.GiaThue = Convert.ToInt32(txtGiaThue.Text)
        _dangtinDTO.TieuDe = txtTieuDe.Text
        _dangtinDTO.SucChua = Convert.ToInt32(cbSuaChua.Text)
        For i As Integer = 0 To tableLoaiNha.Rows.Count - 1
            If tableLoaiNha.Rows(i)("TenLoaiNha").ToString() = cbLoaiNha.Text Then
                _dangtinDTO.MaLoaiNha = Convert.ToInt32(tableLoaiNha.Rows(i)("MaLoaiNha").ToString())
                Exit For
            End If
        Next

        For i As Integer = 0 To tableLoaiTin.Rows.Count - 1
            If tableLoaiTin.Rows(i)("TenLoai").ToString() = cbLoaiTin.Text Then
                _dangtinDTO.MaLoaiTin = Convert.ToInt32(tableLoaiTin.Rows(i)("MaLoaiTin").ToString())
                Exit For
            End If
        Next

        If ckXeBuyt.Checked = True Then
            _dangtinDTO.GanTramXeBuyt = 1
        Else
            _dangtinDTO.GanTramXeBuyt = 0
        End If
        If ckOGhep.Checked = True Then
            _dangtinDTO.OGhep = 1
        Else
            _dangtinDTO.OGhep = 0
        End If
        If ckNauAn.Checked = True Then
            _dangtinDTO.ChoNauAn = 1
        Else
            _dangtinDTO.ChoNauAn = 0
        End If
        If ckChungChu.Checked = True Then
            _dangtinDTO.OChungChu = 1
        Else
            _dangtinDTO.OChungChu = 0
        End If
        If ckInternet.Checked = True Then
            _dangtinDTO.Internet = 1
        Else
            _dangtinDTO.Internet = 0
        End If
        If ckTruyenHinhCap.Checked = True Then
            _dangtinDTO.TruyenHinhCap = 1
        Else
            _dangtinDTO.TruyenHinhCap = 0
        End If
        If ckGanCho.Checked = True Then
            _dangtinDTO.GanCho = 1
        Else
            _dangtinDTO.GanCho = 0
        End If
        If ckChoNamThue.Checked = True Then
            _dangtinDTO.ChoNamThue = 1
        Else
            _dangtinDTO.ChoNamThue = 0
        End If
        If ckChoNuThue.Checked = True Then
            _dangtinDTO.ChoNuThue = 1
        Else
            _dangtinDTO.ChoNuThue = 0
        End If
        If cbTrangThai.Text = "Đã duyệt" Then
            _dangtinDTO.TinhTrang = 1
        ElseIf cbTrangThai.Text = "Chưa duyệt" Then
            _dangtinDTO.TinhTrang = 2
        ElseIf cbTrangThai.Text = "Tạm đóng" Then
            _dangtinDTO.TinhTrang = 3
        ElseIf cbTrangThai.Text = "Đã xóa" Then
            _dangtinDTO.TinhTrang = 4
        End If
        '---------------------------------------------------------------------
        diachiDTO.Duong = txtDuong.Text
        diachiDTO.Phuong = txtPhuong.Text
        diachiDTO.Quan = cbQuan.Text
        diachiDTO.SoNha = txtSoNha.Text
        diachiDTO.MaDiaChi = _dangtinDTO.MaDiaChi
        diachiBUS.Update(diachiDTO)
        '------------------------------------------------------------------------
        _dangtinBUS.Update(_dangtinDTO)
        _tableDangTin = _dangtinBUS.GetAll()
        frmQuanTri.dgvDangTin.DataSource = _tableDangTin
        frmQuanTri.dgvDangTin.Refresh()
        Me.Close()
    End Sub
    Public _tableDangTin As New DataTable

    Private Sub txtGiaThue_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtGiaThue.KeyPress
        If (Not Char.IsNumber(e.KeyChar) And Not Char.IsControl(e.KeyChar)) Then
            e.Handled = True
        End If
    End Sub

    Private Sub txtDienTich_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles txtDienTich.KeyPress
        If (Not Char.IsNumber(e.KeyChar) And Not Char.IsControl(e.KeyChar)) Then
            e.Handled = True
        End If
    End Sub
End Class